var ns, UIAlbumBrowser;
var app = new Vue({
    el: '#app',
    data: {
        ProjectID:0,
        form: {
            ID: 0,
            CheckUserID:'',
            CheckUserName:'',
            CompleteContent: '',
            CheckDesc:'',//验收说明
            Status:0,//验收状态 1-通过 2-不通过
        },
        imglist: [],
        imgindex: 0,
        select_imglist: [],
        select_imgindex: 0,
        maxuploadcount: CONFIG.maxuploadcount,
        controltype:1,//1-整改完成，2-验收
    },
    methods: {
        do_save: function() {
            var that = this;
            if (that.form.IsAllowUpload && that.imglist.length == 0) {
                api.toast({
                    msg: '请上传图片',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            if(that.controltype==1){
                that.do_save_process();
            }else{
                that.do_save_yanshou();
            }
            
        },
        reload_list: function() {
          api.sendEvent({
              name: 'reloadservicelist',
          });
          api.sendEvent({
              name: 'updata_task_con',
          });
        },
        do_save_process: function() {
            var that = this;
            var options = {};
            options.action = "completequalitycontrol";
            options.ID = that.form.ID;
            options.ProcessType=2;//1-处理，2-完工
            options.CheckUserID = that.form.CheckUserID;
            options.CheckUserName = that.form.CheckUserName;
            options.CompleteContent = that.form.CompleteContent;
            var filelist = [];
            for (var i = 0; i < that.imglist.length; i++) {
                if (that.imglist[i].isupload) {
                    filelist.push(that.imglist[i].url);
                }
            }
            ns.post(options, function(succeed, data, err) {
                if (succeed) {
                    api.toast({
                        msg: '操作成功',
                        duration: 2000,
                        location: 'bottom'
                    });
                    that.reload_list();
                    setTimeout(function() {
                        api.closeWin();
                    }, 1000)
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true,
                files: {
                    file: filelist
                }
            });
        },
        //验收
        do_save_yanshou:function(){
            var that = this;
            var options = {};
            options.action = "checkquality";
            options.ID = that.form.ID;
            options.CheckUserID = that.form.CheckUserID;
            options.CheckUserName = that.form.CheckUserName;
            options.Type = that.form.Status;
            options.CheckDesc = that.form.CheckDesc;
            var filelist = [];
            for (var i = 0; i < that.imglist.length; i++) {
                if (that.imglist[i].isupload) {
                    filelist.push(that.imglist[i].url);
                }
            }
            ns.post(options, function(succeed, data, err) {
                if (succeed) {
                    api.toast({
                        msg: '操作成功',
                        duration: 2000,
                        location: 'bottom'
                    });
                    that.reload_list();
                    setTimeout(function() {
                        api.closeWin();
                    }, 1000)
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true,
                files: {
                    file: filelist
                }
            });
        },
        do_select_area:function(status){
            var that=this;
            that.form.Status=status;
        },
        //验收人
        do_choose_user:function(){
            var that=this;
            ns.openWin('quality_chooseperson', '选择验收人', {
                ProjectID: that.ProjectID,
                type:2
            });
        },
        choose_picture: function() {
            var that = this;
            ns.confirmPer('camera', function() {
                that.choose_picture_start();
            })
        },
        choose_picture_start: function() {
            var that = this;
            if (that.imglist.length >= that.maxuploadcount) {
                api.toast({
                    msg: '最多支持' + that.maxuploadcount + '张图片',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            UIAlbumBrowser.imagePicker({
                max: that.maxuploadcount - that.imglist.length,
                styles: {
                    bg: '#000000',
                    mark: {
                        position: 'top_right',
                        size: 20
                    },
                    nav: {
                        bg: '#000000',
                        cancelColor: '#fff',
                        cancelSize: 16,
                        nextStepColor: '#7fff00',
                        nextStepSize: 16
                    },
                    thumbnail: { //（可选项）返回的缩略图配置，**建议本图片不要设置过大** 若已有缩略图，则使用已有的缩略图。若要重新生成缩略图，可先调用清除缓存接口api.clearCache()。
                        w: 100, //（可选项）数字类型；返回的缩略图的宽；默认：原图的宽度
                        h: 100 //（可选项）数字类型；返回的缩略图的宽；默认：原图的高度
                    }
                },
                animation: true,
            }, function(ret) {
                if (ret.eventType == 'nextStep') {
                    if (ret.list && ret.list.length > 0) {
                        that.select_imglist = ret.list;
                        that.select_imgindex = 0;
                        that.UIAlbumBrowser_transPath();
                    }
                    UIAlbumBrowser.closePicker();
                }
                if (ret.originalPath && ret.originalPath.length > 0) {
                    that.imglist.push({
                        ID: 0,
                        index: that.imgindex,
                        url: ret.originalPath,
                        thumbPath: ret.originalPath,
                        isupload: true
                    });
                    that.imgindex++;
                }
            });
        },
        UIAlbumBrowser_transPath: function() {
            var that = this;
            var item = that.select_imglist[that.select_imgindex];
            UIAlbumBrowser.transPath({
                path: item.path
            }, function(ret, err) {
                if (ret) {
                    that.select_imgindex++;
                    if (that.select_imgindex < that.select_imglist.length) {
                        that.UIAlbumBrowser_transPath();
                    } else {
                        that.select_imgindex = 0;
                    }
                    that.imglist.push({
                        ID: 0,
                        index: that.imgindex,
                        url: ret.path,
                        thumbPath: item.thumbPath,
                        isupload: true
                    });
                    that.imgindex++;
                }
            });
        },
        remove_img: function(index) {
            var that = this;
            for (var i = 0; i < that.imglist.length; i++) {
                var item = that.imglist[i];
                if (item.index == index) {
                    that.imglist.splice(i, 1);
                }
            }
        },
        do_viewimages: function(activeIndex) {
            var that = this;
            var imageUrls = [];
            for (var i = 0; i < that.imglist.length; i++) {
                imageUrls.push(that.imglist[i].url);
            }
            var imageBrowser = api.require('imageBrowser');
            imageBrowser.openImages({
                showList: false,
                activeIndex: activeIndex,
                imageUrls: imageUrls
            });
        },
    }
});
apiready = function() {
    api.parseTapmode();
    UIAlbumBrowser = api.require('UIAlbumBrowser');
    ns = window.Foresight.Util;
    app.ProjectID = api.pageParam.ProjectID || 0;
    app.form.ID = api.pageParam.ID || 0;
    app.form.CheckUserID = api.pageParam.CheckUserID || 0;
    app.form.CheckUserName = api.pageParam.CheckUserName;
    //1-整改完成，2-验收
    app.controltype=api.pageParam.type || 0;
    api.addEventListener({
        name: 'do_qualitychoose'
    }, function (ret, err) {
        if (ret) {
            if(ret.value.type==2){
                app.form.CheckUserName=ret.value.item.FinalRealName;
                app.form.CheckUserID=ret.value.item.FinalUserID;
            }
            
        }
    });
    // app.type = api.pageParam.type;
}
